package com.shushan.lockscreen;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.os.Build;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import com.android.permission.FloatPermissionUtil;
import com.shushan.base.Constants;
import com.shushan.ui.FloatToast;
import com.shushan.util.SharedPreferencesUtils;
import com.shushan.util.ToastUtils;

/**
 * @ClassName: GamesWindowManager
 * @Desciption: 可完应用窗口管理类
 * @author: yichaohua
 * @date: 2018-06-04
 */
public class GamesWindowManager {

    private static final String TAG = "GamesWindowManager";

    private static GamesFloatWindow gamesFloatWindow;
    private static WindowManager.LayoutParams gamesWindowParams;
    private static WindowManager gamesWindowManager;
    private static boolean isGamesAvailable = false;


    public static void createGamesFloatWindow(Context context) {
        Log.i(TAG,"createGamesFloatWindow()");

        if(context == null){
            Log.e(TAG,"context == null");
            return;
        }

        if(gamesWindowManager == null){
            gamesWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }

        if(gamesWindowParams == null){
            gamesWindowParams = new WindowManager.LayoutParams();
        }

        if(gamesFloatWindow == null){
            gamesFloatWindow = new GamesFloatWindow(context);
        }else {
            gamesFloatWindow.setQutiGone();
        }

        gamesWindowParams.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){//8.0
            gamesWindowParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        }else {
            gamesWindowParams.type =  WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        }
        gamesWindowParams.format = PixelFormat.TRANSPARENT;
        gamesWindowParams.gravity = Gravity.TOP;
        gamesWindowParams.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN |
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
        gamesWindowParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        gamesWindowParams.height = WindowManager.LayoutParams.MATCH_PARENT;
        //强制竖屏
        gamesWindowParams.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;

        if(FloatPermissionUtil.checkPermission(context)){

            if(isGamesAvailable){
                try {
                    gamesWindowManager.updateViewLayout(gamesFloatWindow, gamesWindowParams);
                }catch (IllegalArgumentException e){
                    e.printStackTrace();
                    gamesWindowManager.addView(gamesFloatWindow, gamesWindowParams);
                }

            }else {
                try {
                    gamesWindowManager.addView(gamesFloatWindow, gamesWindowParams);
                }catch (IllegalStateException e){
                    e.printStackTrace();
                    gamesWindowManager.removeView(gamesFloatWindow);
                    gamesWindowManager.addView(gamesFloatWindow, gamesWindowParams);
                }

            }
            isGamesAvailable = true;
        }else {
            FloatToast.makeText(context,"您的手机没有授予悬浮窗权限，请开启后再试").show();
        }
    }

    public static void removeGamesFloatWindow(Context context) {
        Log.i(TAG,"removeGamesFloatWindow()");
        if(gamesWindowManager == null){
            gamesWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }
        if(isGamesAvailable){
            try {
                isGamesAvailable = false;
                gamesWindowManager.removeView(gamesFloatWindow);
            }catch (IllegalArgumentException e){
                e.printStackTrace();
            }

        }

    }

    public static GamesFloatWindow getGamesFloatWindow() {
        return gamesFloatWindow;
    }

    public static void setGamesFloatWindow(GamesFloatWindow gamesFloatWindow) {
        GamesWindowManager.gamesFloatWindow = gamesFloatWindow;
    }

    public static boolean isIsGamesAvailable() {
        return isGamesAvailable;
    }

    public static void setIsGamesAvailable(boolean isGamesAvailable) {
        GamesWindowManager.isGamesAvailable = isGamesAvailable;
    }
}
